package ar.com.angrymachine.hunter.gameplay;

import java.util.Vector;

import ar.com.angrymachine.hunter.HunterResourceManager;


public class Database
{
    
    public static Vector enemies = new Vector();
    public static Vector traps = new Vector();
    public static Vector chests = new Vector();
    public static Vector rooms = new Vector();
    public static Monster lucifer;
    static
    {
        // load enemy database
        enemies.add(new Monster("Imp", HunterResourceManager.getImage("monster-imp.png"), 200, 8, 1000, 1));
        enemies.add(new Monster("Emberbug", HunterResourceManager.getImage("monster-skorpio.png"), 400, 10, 2000, 2));
        enemies.add(new Monster("Flying Horror", HunterResourceManager.getImage("monster-stingdemon.png"), 600, 15, 2000, 3));
        enemies.add(new Monster("Egglayer", HunterResourceManager.getImage("monster-hermit.png"), 1000, 40, 4000, 4));
        enemies.add(new Monster("Splitter", HunterResourceManager.getImage("monster-splitting.png"), 1000, 40, 4000, 5));
        enemies.add(new Monster("Hunger", HunterResourceManager.getImage("monster-brutedemon.png"), 1200, 100, 8000, 6));
        enemies.add(lucifer = new Monster("Lucifer", HunterResourceManager.getImage("lucifer.png"), 8000, 400, 3000, 100));
        
        // load trap database
        traps.add(new Trap("Poison Trap", HunterResourceManager.getImage("trap.png"), 50, 1));
        traps.add(new Trap("Firebomb Trap", HunterResourceManager.getImage("trap.png"), 80, 2));
        
        // load chest database
        chests.add(new Chest("Wooden Chest",HunterResourceManager.getImage("chest.png"), 1));
        chests.add(new Chest("Sturdy Chest",HunterResourceManager.getImage("chest2.png"), 1));
        
        // load room database
        rooms.add(new Room("Underground Temple", HunterResourceManager.getImage("temple.png"), 0));
        rooms.add(new Room("Catacombs", HunterResourceManager.getImage("cave.png"), 1));
        rooms.add(new Room("Oppresive Cave", HunterResourceManager.getImage("cave-eyes.png"), 2));
        rooms.add(new Room("Infernal Shrine", HunterResourceManager.getImage("cave-red.png"), 3));
        // special rooms
        rooms.add(new Room("Collapsing Cave", HunterResourceManager.getImage("cave.png"), -1));
    }
    
    public static int getLeveledEntityMaxLevel(Vector container)
    {
    	if(container.isEmpty())
    	{
    		return 0;
    	}
        int maxLevel = Integer.MIN_VALUE;
        for (int i = 0; i < container.size(); i++)
        {
            Leveled entity = (Leveled) container.elementAt(i);
            int entityLevel = entity.getLevel();
            if(entityLevel > maxLevel)
            {
                maxLevel = entityLevel;
            }
        }
        return maxLevel;
    }

    public static int getLeveledEntityMinLevel(Vector container)
    {
    	if(container.isEmpty())
    	{
    		return 0;
    	}
        int minLevel = Integer.MAX_VALUE;
        for (int i = 0; i < container.size(); i++)
        {
            Leveled entity = (Leveled) container.elementAt(i);
            int entityLevel = entity.getLevel();
            if(entityLevel < minLevel)
            {
                minLevel = entityLevel; 
            }
        }
        return minLevel;
    }

    public static Leveled getRandomLeveledEntity(int level, Vector container)
    {
    	if(container.size() == 0)
    	{
    		throw new RuntimeException("getRandomLeveledEntity()::Entity requested for a container with no entities.");
    	}
        Leveled newEntity = null;
        // make sure the level requested is within bounds
        int minLevel = getLeveledEntityMinLevel(container);
        int maxLevel = getLeveledEntityMaxLevel(container);
        // clamp the level
        if (level < minLevel)
        {
            level = minLevel;
        }
        if (level > maxLevel)
        {
            level = maxLevel;
        }
        // now select the entity, making sure we pick the closest leveled entity
        // if there is no particular entity with the requested level
        int minReqEntity = level;
        int maxReqEntity = level;
        while(newEntity == null)
        {
	        for (int i = 0; i < container.size(); i++)
	        {
	            Leveled entity = (Leveled) container.elementAt(i);
	            int entityLevel = entity.getLevel();
	            if (entityLevel == minReqEntity || entityLevel == maxReqEntity)
	            {
	                newEntity = entity;
	                break;
	            }
	        }
	        minReqEntity--;
	        maxReqEntity++;
        }
        return newEntity;
    }

}
